package com.salon.server.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.salon.server.entity.User;
import com.salon.server.model.R;
import com.salon.server.model.request.UserRequest;
import com.salon.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 用户基础信息表 前端控制器
 * </p>
 *
 * @author zooy
 * @since 2025-08-26
 */
@RestController
@RequestMapping("/salon/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public R login(@RequestBody User user) {

        return userService.loginValid(user);
    }

    /**
     * 查询
     */
    @PostMapping("/query")
    public R query(@RequestBody User user) {
        return R.success(userService.getById(user.getId()));
    }

    /**
     * 新增
     */
    @PostMapping("/add")
    public R add(@RequestBody User user) {

        user.setNickname(user.getRealName());
        userService.saveRoleUser(user);
        return R.success();
    }

    /**
     * 更新
     */
    @PostMapping("/update")
    public R update(@RequestBody User user) {
        return R.success(userService.updateById(user));
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestBody User user) {
        return R.success(userService.removeById(user.getId()));
    }

    /**
     * 分页查询
     */
    @PostMapping("/page")
    public R page(@RequestBody UserRequest userRequest) {

        return R.success(userService.page(userRequest, new LambdaQueryWrapper<User>()
                .eq(StrUtil.isNotEmpty(userRequest.getUsername()), User::getUsername, userRequest.getUsername())
                .eq(StrUtil.isNotEmpty(userRequest.getRole()), User::getRole, userRequest.getRole())
                .eq(userRequest.getStatus() != null, User::getStatus, userRequest.getStatus())
                .eq(StrUtil.isNotEmpty(userRequest.getNickname()), User::getNickname, userRequest.getNickname())
        ));
    }

    /**
     * resetPassword
     */
    @PostMapping("/resetPassword")
    public R resetPassword(@RequestBody UserRequest userRequest) {
        userService.resetPassword(userRequest);
        return R.success();
    }

}
